package com.qifan.alb2b.action.front;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.UserInfoService;
import com.qifan.alb2b.service.vo.UserVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.Stringer;

@Controller
@RequestMapping("/user")
public class LoginController{
	private UserInfoService userInfoService;

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	@RequestMapping("/login.usr")
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request,@RequestParam("userName") String userName, @RequestParam("userPwd") String userPwd, @RequestParam("vcode") String vcode) {
		 String sessionVcode =
			 ""+request.getSession(true).getAttribute("TAG_VCODE");
		 if(!sessionVcode.equalsIgnoreCase(vcode.toUpperCase())){
			 throw new BusinessException("验证码输入错误！");
			 }
		 Map<String, Object> map=new HashMap<String, Object>();
		 UserVo userVo  =userInfoService.getUser(userName,Stringer.md5(userPwd).substring(0,30));
		if(userVo!=null){
			request.getSession().setAttribute(Constants.SESSION_USER_VO,userVo);
			map.put("success", true);		
		}
		return map;
	}
	
	/**
	 * 注销，返回登录页面
	 * @return
	 */
	@RequestMapping("/logout.usr")
	public String logout(HttpSession session){
		session.invalidate();
		return "pages/front/index";
	}
	
	/**
	 * 注销，返回首页
	 * @return
	 */
	@RequestMapping("/ajaxlogout.usr")
	@ResponseBody
	public Map<String, Object> ajaxlogout(HttpSession session){
		session.invalidate();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
}
